package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserMessageSmsLog;
import com.frogs.ucenter.service.UserMessageSmsLogService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin/userMessageSmsLog")
public class UserMessageSmsLogController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userMessageSmsLog";

    private static final String HOME_VIEW = "admin/userMessageSmsLog/home";

    private static final String EDIT_VIEW = "admin/userMessageSmsLog/edit";

    @Autowired
    private UserMessageSmsLogService userMessageSmsLogService;

    @RequestMapping()
    public String listUserMessageSmsLog(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserMessageSmsLog(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userMessageSmsLogService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserMessageSmsLog(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserMessageSmsLog userMessageSmsLog) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            userMessageSmsLogService.insert( userMessageSmsLog ); 
            builder = DWZResponse.getSucessBuilder("success");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserMessageSmsLog(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userMessageSmsLogService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserMessageSmsLog(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserMessageSmsLog userMessageSmsLogNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserMessageSmsLog userMessageSmsLog = userMessageSmsLogService.getById( id ); 
                PropertyUtils.copyProperties(userMessageSmsLog, userMessageSmsLogNew);
                userMessageSmsLogService.update( userMessageSmsLog ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("success");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(AUDIT)
    public String auditUserMessageSmsLog(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer status = reqInfo.getIntParameter( "status", -1 );
        
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userMessageSmsLogService.update( model ); 
            builder = DWZResponse.getSucessBuilder("操作成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("操作失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserMessageSmsLog(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userMessageSmsLogService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<UserMessageSmsLog> getPageList(ModelMap model) {
        PageList<UserMessageSmsLog> userMessageSmsLogList = userMessageSmsLogService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userMessageSmsLogList",userMessageSmsLogList);
        model.put("pageTurn",userMessageSmsLogList.getPageTurn());
        return userMessageSmsLogList;
    }
}